/*  This file is part of CASpp.

    CASpp is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    CASpp is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with CASpp.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2014 Jérome LAURENS

    See https://code.google.com/p/idalgo/

*/

#include <iostream>
#include "boolean.h"

CASPP_NS_BEGIN

Boolean::Boolean(const Values v)
        : value_m(v) {
}
Boolean::Boolean(const bool yorn)
        : value_m(yorn? kTrue: kFalse){
}
void Boolean::init(const std::string & s) {
    if (s == "Vrai") {
        value_m = kTrue;
    } else if (s == "Faux") {
        value_m = kFalse;
    } else {
        throw std::runtime_error(std::string("Bad Boolean argument:<")+s+">");
    }
}
Boolean::Boolean(const char * s) {
    init(std::string(s));
}
Boolean::Boolean(const std::string & s) {
    init(s);
}

bool Boolean::isTrue() const {
    return value_m == kTrue;
}
bool Boolean::isFalse() const {
    return value_m == kFalse;
}
bool Boolean::isUnknown() const {
    return value_m == kUnknown;
}
bool Boolean::isUndefined() const {
    return value_m == kUndefined;
}
Boolean Boolean::operator ! () const {
    if (value_m == kUndefined) {
        return kUndefined;
    }
    if (value_m == kUnknown) {
        return kUnknown;
    }
    return value_m == kTrue? kFalse:kTrue;
}
Boolean Boolean::operator && (const Boolean & rhs) const {
    if (value_m == kUndefined || rhs.value_m == kUndefined) {
        return kUndefined;
    }
    if (value_m == kUnknown || rhs.value_m == kUnknown) {
        return kUnknown;
    }
    return value_m == kTrue && rhs.value_m == kTrue? kTrue:kFalse;
}
Boolean Boolean::operator && (const bool & rhs) const {
    if (value_m == kUndefined) {
        return kUndefined;
    }
    if (value_m == kUnknown) {
        return kUnknown;
    }
    return rhs && value_m == kTrue? kTrue:kFalse;
}
Boolean Boolean::operator || (const Boolean & rhs) const {
    if (value_m == kUndefined || rhs.value_m == kUndefined) {
        return kUndefined;
    }
    if (value_m == kUnknown || rhs.value_m == kUnknown) {
        return kUnknown;
    }
    return value_m == kTrue || rhs.value_m == kTrue? kTrue:kFalse;
}
Boolean Boolean::operator || (const bool & rhs) const {
    if (value_m == kUndefined) {
        return kUndefined;
    }
    if (value_m == kUnknown) {
        return kUnknown;
    }
    return rhs || value_m == kTrue? kTrue:kFalse;
}

std::string Boolean::description() const {
    return isTrue()? "Vrai":(isFalse()? "Faux":(isUnknown()? "Inconnu": "Indéfini"));
}
Boolean::Values Boolean::value() const {
    return value_m;
}

Boolean operator && (const bool & lhs, const Boolean & rhs) {
    return rhs && lhs;
}
Boolean operator || (const bool & lhs, const Boolean & rhs) {
    return rhs || lhs;
}
bool isTrue(const Boolean & b) {
    return b.isTrue();
}
bool isFalse(const Boolean & b) {
    return b.isFalse();
}
bool isUnknown(const Boolean & b) {
    return b.isUnknown();
}
bool isUndefined(const Boolean & b) {
    return b.isUndefined();
}

CASPP_NS_END

